package com.yanqu.road.dao.impl.union.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionActivityLockRank;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionActivityLockRankDaoImpl extends TempDataObjectDao<CrossUnionActivityLockRank> {


    public static String getCrossUnionActivityLockRankSql = "select * from `t_c_activity_union_lock_rank` where `endTime`>? order by `rank` ";

    public Map<Integer, Map<Integer, List<CrossUnionActivityLockRank>>> getCrossUnionActivityLockRank(int endTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, List<CrossUnionActivityLockRank>>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionActivityLockRankSql);
            int i = 1;
            call.setInt(i++, endTime);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionActivityLockRank data = parse(rs);
                if(!result.containsKey(data.getActivityId())){
                    result.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                if(!result.get(data.getActivityId()).containsKey(data.getGroupId())){
                    result.get(data.getActivityId()).put(data.getGroupId(), new ArrayList<>());
                }
                result.get(data.getActivityId()).get(data.getGroupId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertCrossUnionActivityLockRankSql = "insert into `t_c_activity_union_lock_rank`(activityId,groupId,rank,unionUid,unionBaseInfo,serverList,endTime) values (?,?,?,?,?,?,?)";
    
    @Override
    protected boolean addData(CrossUnionActivityLockRank data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndTime()));
        if (getDb().executeNoneQuery(insertCrossUnionActivityLockRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossUnionActivityLockRankSql = "update `t_c_activity_union_lock_rank` set unionUid=?,unionBaseInfo=?,serverList=?,endTime=? where activityId=? and groupId=? and rank=?";
    
    @Override
    protected boolean updateData(CrossUnionActivityLockRank data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRank()));
        if (getDb().executeNoneQuery(updateCrossUnionActivityLockRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUnionActivityLockRank parseData(ResultSet rs) throws SQLException {
        CrossUnionActivityLockRank rank = new CrossUnionActivityLockRank();
        rank.setActivityId(rs.getInt("activityId"));
        rank.setGroupId(rs.getInt("groupId"));
        rank.setRank(rs.getInt("rank"));
        rank.setUnionUid(rs.getString("unionUid"));
        rank.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        rank.setServerList(JSON.parseObject(rs.getString("serverList"), new TypeReference<List<Long>>() {}));
        rank.setEndTime(rs.getInt("endTime"));

        return rank;
    }
}
